package cn.wu.reggie.controller;

import cn.wu.reggie.common.Page;
import cn.wu.reggie.common.R;
import cn.wu.reggie.dto.DishDto;
import cn.wu.reggie.entity.Dish;
import cn.wu.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("dish")
public class DishController {


    @Autowired
    private DishService dishService;


    @GetMapping("page")
    public R findByPage(@RequestParam("page") Integer pageNum,Integer pageSize,String name){
        // 调用service完成分页查询操作
        R<Page<Dish>> pages = dishService.findByPage(pageNum,pageSize,name);
        return pages;
    }

    @PostMapping
    public R save(@RequestBody DishDto dishDto, HttpSession session){
        // 1 获取更新人
        Long userId = (Long) session.getAttribute("employee");
        // 2 给dishDto封装创建人和修改人id
        dishDto.setCreateUser(userId);
        dishDto.setUpdateUser(userId);
        // 3 调用service完成保存
        return dishService.save(dishDto);
    }

    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        return dishService.findById(id);
    }


    @PutMapping
    public R update(@RequestBody DishDto dishDto,HttpSession session){

        Long userId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(userId);
        return dishService.update(dishDto);

    }

    @GetMapping("list")
    public R findByCategoryId(Long categoryId){
        return dishService.findByCategoryId(categoryId);
    }

//    @PostMapping("status/{status}")
//    public R checkStatus0(@RequestParam("ids") List<Long> ids){
//        return dishService.checkStatus0(ids);
//    }

}
